﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.CarModel;
using Aliyeye.Car.Net.UI.PrintExport;

namespace Aliyeye.Car.Net.UI.OrderModel
{
    public partial class OrderInfoView : Aliyeye.Car.Net.UI.BaseForm
    {
        private string strID = "";
        DataSet dsCars = null;

        public OrderInfoView()
        {
            InitializeComponent();
        }

        public OrderInfoView(string id)
        {
            InitializeComponent();
            strID = id;
            BindInfo();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BindInfo()
        {
            string _strSql = "1=1 ";
            string strWhere = " and Reserve.Id='" + strID + "'";
            string strOrderBy = " order by Reserve.InpuTime desc";
            string strSelect = "select DriverUser.TrueName as DriverName, OrderUser.TrueName as OrderInputName,Reserve.Id,Reserve.LivAddr,Reserve.AriAddr,Reserve.SenAddr,Reserve.FlayCode,Reserve.PriceType1,Reserve.PriceType2,Reserve.Marks,Reserve.Code as OrderCode, Reserve.InpuTime,Reserve.Type, Reserve.BookTime , Reserve.EndBooktime , Reserve.OrderSataus,Customer.Code as CustomerCode,Customer.ComName,Customer.Name,Customer.Sex ,Customer.Phone ,Customer.Tel ,Customer.Custype ,  Car.Code,Car.DayLimitKM, Car.Brand,Car.Cartype, Car.Color, Car.DayRent, Car.InsureTime, Car.InsureEnd, Car.OvertimePrie, Car.TimeoutPrie,Accounts_Users.TrueName from Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Car.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId left join Accounts_Users OrderUser on OrderUser.UserId=Reserve.UserId ";
            string strSql = strSelect + " where " + _strSql + strWhere.ToString() + strOrderBy;

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();


            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsCars = (DataSet)result.Result;
                        if (dsCars != null && dsCars.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = dsCars.Tables[0].Rows[0];
                            lblOrderCode.Text = dr["OrderCode"].ToString();
                            lblInpuTime.Text = Convert.ToDateTime(dr["InpuTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");

                            lblCustomerCode.Text = dr["CustomerCode"].ToString();
                            lblCustomerName.Text = dr["Name"].ToString();
                            lblCustomerPhone.Text = dr["Phone"].ToString();
                            lblCustomerSex.Text = dr["Sex"].ToString();
                            lblCustomerTel.Text = dr["Tel"].ToString();
                            lblCustomerType.Text = dr["Custype"].ToString().Trim() == "1" ? "个人" : "企业";
                            lblCarBrand.Text = dr["Brand"].ToString();
                            lblCarCode.Text = dr["Code"].ToString();
                            lblCarColor.Text = dr["Color"].ToString();
                            lblBookTime.Text = Convert.ToDateTime(dr["BookTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                            lblCarType.Text = dr["Cartype"].ToString();
                            lblDayKm.Text = dr["DayLimitKM"].ToString();
                            lblDayRent.Text = dr["DayRent"].ToString();
                            lblDriverName.Text = dr["DriverName"].ToString();
                            lblEndBooktime.Text = Convert.ToDateTime(dr["EndBooktime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                            string type = dr["Type"].ToString();
                            switch (type)
                            {
                                case "1":
                                    lblOrderType.Text = "长租自驾";
                                    break;
                                case "2":
                                    lblOrderType.Text = "短租自驾";
                                    break;
                                case "3":
                                    lblOrderType.Text = "长租带驾";
                                    break;
                                case "4":
                                    lblOrderType.Text = "短租带驾";
                                    break;
                            }
                            lblOvertimePrie.Text = dr["OvertimePrie"].ToString();
                            lblTimeoutPrie.Text = dr["TimeoutPrie"].ToString();

                            lblOrderInputName.Text = dr["OrderInputName"].ToString();
                            lblOrderSataus.Text = dr["OrderSataus"].ToString().Trim().ToLower() == "true" ? "已结算" : "未结算";

                            lblLivAddr.Text = dr["LivAddr"].ToString();
                            lblAriAddr.Text = dr["AriAddr"].ToString();
                            lblSenAddr.Text = dr["SenAddr"].ToString();
                            lblFlayCode.Text = dr["FlayCode"].ToString();
                            lblPriceType.Text = dr["PriceType1"].ToString() + " " + dr["PriceType2"].ToString();
                            lblMarks.Text = dr["Marks"].ToString();
                        }

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                    }
                },
                this
            );
        }

        private void btnPrintContract_Click(object sender, EventArgs e)
        {


            //if (wordHelp.PrintOrderInfoView(dsCars))
            //{
            //    PageCommon.Notify("打印成功！");
            //    overlay.FadeOut();Cursor.Current = Cursors.Default;
            //}
            //else
            //{
            //    PageCommon.Alert("打印失败！");
            //    overlay.FadeOut();Cursor.Current = Cursors.Default;
            //}

            DataSet ds = null;
            CompanySvc.SoapHeaderClass sopaHeaderCompany = new CompanySvc.SoapHeaderClass();
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeaderCompany, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            ds = (DataSet)result.Result;
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                Model.Company model = new Model.Company();

                                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                                model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();

                                //开始打印派车单
                                WordHelper wordHelp = new WordHelper();
                                if (wordHelp.PrintOrderInfoView(model, dsCars))
                                {
                                    PageCommon.Notify("打印成功！");
                                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                                else
                                {
                                    PageCommon.Alert("打印失败！");
                                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                            }
                            else
                            {
                                PageCommon.Alert("打印失败！");
                                overlay.FadeOut();Cursor.Current = Cursors.Default;
                            }
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
        }


    }
}
